package com.tcoiss.datafactory.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.core.web.page.TableDataInfo;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import com.tcoiss.datafactory.domain.MqSyncRecord;
import com.tcoiss.datafactory.service.IMqSyncRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 消息同步记录Controller
 * 
 * @author zw
 * @date 2021-05-13
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mqrecord" )
public class MqSyncRecordController extends BaseController {

    private final IMqSyncRecordService iMqSyncRecordService;

    /**
     * 查询消息同步记录列表
     */
    @PreAuthorize(hasPermi = "mq:mqrecord:list")
    @GetMapping("/list")
    public TableDataInfo list(MqSyncRecord mqSyncRecord) {
        startPage();
        List<MqSyncRecord> list = iMqSyncRecordService.queryList(mqSyncRecord);
        return getDataTable(list);
    }

    /**
     * 导出消息同步记录列表
     */
    @PreAuthorize(hasPermi = "mq:mqrecord:export" )
    @Log(title = "消息同步记录" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,MqSyncRecord mqSyncRecord) throws IOException
    {
        List<MqSyncRecord> list = iMqSyncRecordService.queryList(mqSyncRecord);
        ExcelUtil<MqSyncRecord> util = new ExcelUtil<MqSyncRecord>(MqSyncRecord.class);
        util.exportExcel(response,list, "mqrecord" );
    }

    /**
     * 获取消息同步记录详细信息
     */
    @PreAuthorize(hasPermi = "mq:mqrecord:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iMqSyncRecordService.getById(id));
    }

    /**
     * 新增消息同步记录
     */
    @PreAuthorize(hasPermi = "mq:mqrecord:add" )
    @Log(title = "消息同步记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MqSyncRecord mqSyncRecord) {
        return toAjax(iMqSyncRecordService.save(mqSyncRecord) ? 1 : 0);
    }

    /**
     * 修改消息同步记录
     */
    @PreAuthorize(hasPermi = "mq:mqrecord:edit" )
    @Log(title = "消息同步记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MqSyncRecord mqSyncRecord) {
        return toAjax(iMqSyncRecordService.updateById(mqSyncRecord) ? 1 : 0);
    }

    /**
     * 删除消息同步记录
     */
    @PreAuthorize(hasPermi = "mq:mqrecord:remove" )
    @Log(title = "消息同步记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iMqSyncRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 删除消息同步记录
     */
    @PreAuthorize(hasPermi = "mq:mqrecord:remove" )
    @Log(title = "消息同步记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/clean" )
    public AjaxResult handleDeleteAll() {
        return toAjax(iMqSyncRecordService.remove(new QueryWrapper<>()) ? 1 : 0);
    }
}
